package com.tbit.uqbike.client.service.pushMsg.impl;

import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.WeixinConstant;
import com.tbit.uqbike.client.dao.core.*;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.service.CacheService;
import com.tbit.uqbike.client.service.WeixinPayService;
import com.tbit.uqbike.client.service.pushMsg.WxPushService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 微信服务消息推送
 *
 * @author <a href="mailto:zhang.zhiliang@tbit.com.cn">Zhang ZhiLiang</a>
 * @version 1.0
 * @see <a href="http://adsl1.tbit.com.cn:21490/pages/viewpage.action?pageId=82182491">订单号规范</a>
 * @since 2024/10/16
 */
@Service
public class WxPushServiceImpl implements WxPushService {

    private final MachineDao machineDao;
    private final RideLogDao rideLogDao;
    private final CacheService cacheService;
    private final UserDao userDao;
    private final WeixinPayService weixinPayService;

    public WxPushServiceImpl(MachineDao machineDao, RideLogDao rideLogDao, CacheService cacheService, UserDao userDao, WeixinPayService weixinPayService) {
        this.machineDao = machineDao;
        this.rideLogDao = rideLogDao;
        this.cacheService = cacheService;
        this.userDao = userDao;
        this.weixinPayService = weixinPayService;
    }

    @Override
    public void pushNotReturnBikeMsg(String orderNo) {
        RideLog rideLog = rideLogDao.getByOrderNO(orderNo);
        AccountConfigExt accountConfigExt = cacheService.getAccountConfigExtByKey(rideLog.getAccountId(),
                AccountConfigExtConstant.ACCOUNTCONFIG_MODEL);
        if (accountConfigExt == null || !"1".equals(accountConfigExt.getParamValue())) {
            return;
        }
        Machine machine = machineDao.getByMachineId(rideLog.getMachineId());
//        AdAccountFee adAccountFee = cacheService.getAdAccountFee(machine.getAccountId(), machine.getMachineId());
        // 推送微信服务消息
        Map<String, String> map = new HashMap<>();
        // 温馨提示
        map.put("thing3","车辆临停中，请及时还车，避免产生额外费用");
        // 车辆编号
        map.put("character_string5",machine.getUserCode());
        // 借车时间
        map.put("time4",rideLog.getStartTime());
        // 计费规则
//        StringBuilder stringBuilder = new StringBuilder();
//        int baseTime = Optional.ofNullable(adAccountFee.getBaseTime()).orElse(0);
//        int baseTimeFee = Optional.ofNullable(adAccountFee.getBaseTimeFee()).orElse(0);
//        int overTime = Optional.ofNullable(adAccountFee.getOverTime()).orElse(0);
//        int overTimeFee = Optional.ofNullable(adAccountFee.getOverTimeFee()).orElse(0);
//        int baseMile = Optional.ofNullable(adAccountFee.getBaseMile()).orElse(0);
//        int baseMileFee = Optional.ofNullable(adAccountFee.getBaseMileFee()).orElse(0);
//        int overMile = Optional.ofNullable(adAccountFee.getOverMile()).orElse(0);
//        int overMileFee = Optional.ofNullable(adAccountFee.getOverMileFee()).orElse(0);

//        if (baseTimeFee != 0 || overTimeFee != 0){
//            stringBuilder.append("计费规则：计时费：").append(baseTime).append("分钟内").append(baseTimeFee*1.0/100).append("元，")
//                    .append("超过").append(overTimeFee*1.0/100).append("元/").append(overTime).append("分钟计费\n");
//        }
//        if (baseMileFee != 0 || overMileFee != 0){
//            stringBuilder.append("计费规则：里程费：").append(baseMile).append("公里内").append(baseMileFee*1.0/100).append("元，")
//                    .append("超过").append(overMileFee*1.0/100).append("元/").append(overMile).append("公里计费\n");
//        }
//        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
        String substring = "请前往小程序，查看具体的计费规则";
        map.put("thing2",substring);
        // 锁车说明
        map.put("thing1","请前往小程序，点击【还车】按钮");
        User user = userDao.getByUserId(rideLog.getUserId());
        weixinPayService.sendLockMsg(user.getAccountId(), user.getUserId(), map , WeixinConstant.TEMPLATE_TYPE_NOT_RETURN_BIKE, null);
    }

    @Override
    public void pushNotPayOrderMsg(String orderNo) {
        RideLog rideLog = rideLogDao.getByOrderNO(orderNo);
        User user = userDao.getByUserId(rideLog.getUserId());
        Map<String,String> map = new HashMap<>();
        map.put("character_string6",rideLog.getOrderNO());
        map.put("amount3",rideLog.getMoney()/100+"元");
        map.put("number7",rideLog.getUserCode());
        String startTime = rideLog.getStartTime();
        String endTime = rideLog.getEndTime();
        long startMills = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
        long endMills = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
        int seconds = (int) ((endMills - startMills)/1000);
        int minutes = seconds/60;
        if ((seconds)%60 != 0){
            minutes = minutes + 1;
        }
        // 计算骑行时间
        map.put("thing5",minutes+"分钟");
        map.put("time1",rideLog.getStartTime());
        weixinPayService.sendLockMsg(user.getAccountId(), user.getUserId(), map,WeixinConstant.TEMPLATE_TYPE_NOT_PAY,null);
    }
}
